
//分页变量
var total_page=1;       //总页数
var current_page=1;     //当前页
function detail(institution_name) {
    window.localStorage.setItem("institution_name",institution_name);
    //console.log(institution_name);
}
$(document).ready(function(){
    //初始化请求数据
    requestData();

    window.localStorage.removeItem("institution_name");

    function requestData(){
        var institution_name = $('#institution_name').val().trim();

        var newsList=$(".news-list");
        newsList.html('<ul class="news-list"></ul>');
        var data={
            pageNum : current_page,
            pageSize : 10, //页容量，默认为10
            bankName :institution_name
        };

        $.ajax({
            type:"POST",
            url:'http://59.110.114.107:8888/bank/getAllBankInformation',
            data: data,
            success:function (result) {
                //console.log(result.data);
                if(result.meta.success){
                    //console.log(result.meta.message);
                    if(result.data!=null){
                        total_page=result.data["Total Page Num"];
                        result.data["governmentPolicies List"].map(function (item,index) {
                            newsList.append('<li class="news_item"><h3 style="display: inline"><a href="institutionDetail1.html"  onclick="detail(\''+item.bankName+'\')">'+item.bankName+'</a></h3><span class="page-street">地址：'+item.street+'</span><span class="page-desc">描述：'+item.description+'</span></li>')
                        })
                    }else{
                        alert(result.meta.message);
                    }

                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        })
    }


    $('#search-btn').click(function(){

        var institution_name = $('#institution_name').val().trim();

        var newsList=$(".news-list");
        newsList.html('<ul class="news-list"></ul>');
        var data={
            bankName :institution_name
        };

        $.ajax({
            type:"POST",
            url:'http://59.110.114.107:8888/bank/getAllBankInformation',
            data: data,
            success:function (result) {
                //console.log(result.data);
                if(result.meta.success){
                    //console.log(result.meta.message);
                    if(result.data!=null){
                        total_page=result.data["Total Page Num"];
                        result.data["governmentPolicies List"].map(function (item,index) {
                            newsList.append('<li class="news_item"><h3 style="display: inline"><a href="institutionDetail1.html"  onclick="detail(\''+item.bankName+'\')">'+item.bankName+'</a></h3><span class="page-street">地址：'+item.street+'</span><span class="page-desc">描述：'+item.description+'</span></li>')
                        })
                    }else{
                        alert(result.meta.message);
                    }

                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        })

    });


    //上一页 判断上一页是否存在,若存在将上一页的页码发给后端请求上一页数据
    $('.page-prev').click(function () {
        //判断上一页存在
        if(current_page>1){
            current_page--;
            requestData();
        }
        else{
            alert("没有该页数据");
        }
    });
    //下一页 判断下一页是否存在，若存在将下一页页码发给后端请求下一页数据
    $('.page-next').click(function () {
        //判断下一页存在
        if(current_page<total_page){
            current_page++;
            requestData();
        }
        else{
            alert("没有该页数据");
        }
    });

});